package com.init.iblog.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Bean拷贝工具类
 */
public class BeanCopyUtils {

    private BeanCopyUtils() {
    }

    /**
     * 单个bean拷贝
     * @param source
     * @param clazz
     * @param <V>
     * @return
     */
    public static <V> V copyBean(Object source,Class<V> clazz){
        //创建目标对象(反射)
        V result = null;
        try {
            result = clazz.newInstance();
            //实现属性copy
            BeanUtils.copyProperties(source, result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 多个bean拷贝
     * @param list
     * @param clazz
     * @param <V>
     * @return
     */
    public static <O,V> List<V> copyBeanList(List<O> list,Class<V> clazz){
        return  list.stream()
                .map(o -> copyBean(o,clazz))
                .collect(Collectors.toList());
    }
}
